package com.cwnu.sw.cys.core.common.thread;

import lombok.AllArgsConstructor;

import java.util.concurrent.ThreadFactory;

/**
 * @author jisam
 * @version 1.0
 * @contact "jisamchan@outlook.com"
 * @date 02/03/2025
 * @description 装饰器模式新建线程工厂
 */
@AllArgsConstructor
public class MyThreadFactory implements ThreadFactory {
    private static final MyUncaughtExceptionHandler UNCAUGHT_EXCEPTION_HANDLER = new MyUncaughtExceptionHandler();
    private ThreadFactory originalThreadFactory;
    /**
     * @param r a runnable to be executed by new thread instance
     * @description 额外装饰我们需要的线程
     * @return
     */
    @Override
    public Thread newThread(Runnable r) {
       Thread thread = originalThreadFactory.newThread(r);
       thread.setUncaughtExceptionHandler(UNCAUGHT_EXCEPTION_HANDLER);
       return thread;
    }
}
